package org.rujue.modules.sys.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.rujue.common.config.Global;
import org.rujue.common.persistence.Page;
import org.rujue.common.utils.FileUtils;
import org.rujue.common.utils.StringUtils;
import org.rujue.common.web.BaseController;
import org.rujue.common.web.ResponseResult;
import org.rujue.modules.sys.entity.SysFile;
import org.rujue.modules.sys.service.SysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 通用附件管理Controller
 * @author Mr.xu
 * @version 2023-11-13
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysFile")
public class SysFileController extends BaseController {

	@Autowired
	private SysFileService sysFileService;
	
	@ModelAttribute("sysFile")
	public SysFile get(@RequestParam(required=false) String id) {
		SysFile entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysFileService.get(id);
		}
		if (entity == null){
			entity = new SysFile();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:sysFile:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysFile sysFile, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SysFile> page = sysFileService.findPage(new Page<SysFile>(request, response), sysFile); 
		model.addAttribute("page", page);
		return "modules/sys/file/fileList";
	}

	@RequiresPermissions("sys:sysFile:view")
	@RequestMapping(value = "form")
	public String form(SysFile sysFile, Model model) {
		model.addAttribute("sysFile", sysFile);
		return "modules/sys/file/fileForm";
	}

	@RequiresPermissions("sys:sysFile:edit")
	@RequestMapping(value = "save")
	public String save(SysFile sysFile, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sysFile)){
			return form(sysFile, model);
		}
		sysFileService.save(sysFile);
		addMessage(redirectAttributes, "保存附件成功");
		return "redirect:"+Global.getAdminPath()+"/sys/sysFile/?repage";
	}
	
	@RequiresPermissions("sys:sysFile:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public ResponseResult delete(String[] ids) {
		if(ids.length < 1){
			return ResponseResult.error("操作失效，没有选中数据！");
		}
		try {
			for(String id : ids){
				SysFile sysFile = new SysFile();
				sysFile.setId(id);
				sysFileService.delete(sysFile);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseResult.error("删除失败");
		}
		return ResponseResult.success("删除成功");
	}

	@RequiresPermissions("sys:sysFile:edit")
	@RequestMapping( "/add")
	public String add(SysFile sysFile, Model model) {
		model.addAttribute("sysFile", sysFile);
		List<SysFile> filesList = sysFileService.findList(sysFile);
		model.addAttribute("filesList", filesList);
		return "modules/sys/file/fileAdd";
	}

	@RequiresPermissions("sys:sysFile:edit")
	@RequestMapping( "/save2Upload")
	public String save2Upload(SysFile sysFile, Model model, RedirectAttributes redirectAttributes) {
		sysFileService.save2Upload(sysFile);
		addMessage(redirectAttributes, "保存附件成功");
		redirectAttributes.addAttribute("hostId",sysFile.getHostId());
		redirectAttributes.addAttribute("hostIds",sysFile.getHostIds());
		redirectAttributes.addAttribute("hostTable",sysFile.getHostTable());
		return String.format("redirect:%s/sys/sysFile/add?repage", Global.getAdminPath());
	}

	/**
	 * 在线预览
	 */
	@RequestMapping("/preview")
	public String preview(SysFile sysFile) {
		String relativePath = sysFile.getFilePath();
		return String.format("redirect:/static/assets/bootstrap/js/plugins/pdfjs/web/viewer.html?file=/upload/%s",relativePath);
	}

	@RequestMapping( "/download")
	public void download(SysFile sysFile,HttpServletRequest request,HttpServletResponse response) {
		File file = new File(Global.getConfig("upload-path") +  sysFile.getFilePath());
		FileUtils.downFile(file, request, response);
	}
}